package com.yanqu.road.server.cmd.activity;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.entity.union.cross.CrossUnionMember;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.commonactivity.CommonActivityProto;
import com.yanqu.road.pb.rank.RankProto;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.manger.ServerListMgr;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.union.CrossUnionMgr;
import com.yanqu.road.server.protocol.ChannelProtocol;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.string.StringUtils;

import java.math.BigInteger;
import java.util.Map;

@Cmd(code = Protocol.S_CHANNEL_UNION_MAX_ABILITY_RANK_LIST, desc = "请求巅峰跨域赚钱排行")
public class ChannelPeakActivityUnionMaxAbilityRankListCmd extends AbstractCommand {

    private int activityId;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        CommonActivityProto.CommonActivityRankListReqMsg reqMsg = CommonActivityProto.CommonActivityRankListReqMsg.parseFrom(packet.getBytes());
        activityId = reqMsg.getActivityId();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        ActivityInfo activityInfo = ActivityMgr.getOpenActivityInfo(activityId);
        if (null == activityInfo) {
            return;
        }

        if (activityInfo.getType() != eActivityType.ChannelTonBingActivity.getValue()) {
            return;
        }

        RankProto.GetActivityUnionRankListRespMsg.Builder respMsg = RankProto.GetActivityUnionRankListRespMsg.newBuilder();
        respMsg.setActivityId(activityId);
        respMsg.setMyValue("0");
        respMsg.setMyRank(0);
        String unionUid = player.getUserInfo().getUnionUid();
        if(!StringUtils.isNullOrEmpty(unionUid)){
            Map<Long, CrossUnionMember> memberMap = CrossUnionMgr.getUnionMemberMap(unionUid);
            BigInteger myValue = BigInteger.ZERO;
            for (CrossUnionMember member : memberMap.values()) {
                myValue = myValue.add(member.getHistoryMaxAbility());
            }
            respMsg.setMyValue(myValue.toString());
            respMsg.setUnionId(unionUid);
        }
        respMsg.setServerId(GameServer.getInstance().getServerId());
        respMsg.setChannelId(ServerListMgr.getChannelId(GameServer.getInstance().getServerId()));

        player.sendChannelPlayerPacket(ChannelProtocol.N_UNION_GET_MAX_ABILITY_UNION_RANK_LIST, respMsg);
    }

    @Override
    public void sendData(GamePlayer player) {

    }
}
